"""author: wenyao
   data: 2021/3/28
   project: devopscmdb
"""
import re
#1、 search match 查找第一个匹配
#search 接受一个正则表达式和字符串，返回字符串中的第一个匹配
#match  从字符串头开始查找匹配，只匹配字符串的开始

# >>> re.search("sanchuang","hello, this is sanchuang!")
# <_sre.SRE_Match object; span=(15, 24), match='sanchuang'>
# >>> re.search("sanchuang","hello, this is sanchuang! sanchuang!")
# <_sre.SRE_Match object; span=(15, 24), match='sanchuang'>
# >>> result = re.search("sanchuang","hello, this is sanchuang! sanchuang!")
# >>> re.match("sanchuang","hello, this is sanchuang! sanchuang!")
# >>> re.match("sanchuang","sanchuang hello, this is sanchuang! sanchuang!")
# <_sre.SRE_Match object; span=(0, 9), match='sanchuang'>

# #######正则表达式中推荐使用原始字符串
# r'sanle' 中的r代表的是raw（原始字符串）
# • 原始字符串与正常字符串的区别是原始字符串不会将\字符解释成一个转义字符
# • 正则表达式使用原始字符很常见且有用
# >>> re.search("\\tsanle",'hello \\tsanle')
# >>> re.search(r"\\tsanle",'hello \\tsanle')
# <_sre.SRE_Match object; span=(6, 13), match='\\tsanle'>
# >>> re.search(r"\\\tsanle",'hello \\tsanle')
# >>> re.search("\\\tsanle",'hello \\tsanle')
# >>> re.search("\\\tsanle",'hello \\\tsanle')
# <_sre.SRE_Match object; span=(7, 13), match='\tsanle'>

########## match对象的使用###################
# match.group(default=0)：返回匹配的字符串。
# • group是由于正则表达式可以分拆为多个只调出匹配子集的子组。
# • 0是默认参数，表示匹配的整个串，n 表示第n个分组
#
# >>> msg = "It's raining cats and dogs, cats1 cats2"
# >>> match = re.search(r"cats", msg)
# >>> match.group()
# 'cats'
# >>> match.group(0)
# 'cats'
# >>> match.start()
# 13
# >>> match.end()
# 17
# >>> match.groups()
# ()

######## findall  finditer
# msg = "It's raining cats and dogs, cats1 cats2"
# print(re.findall(r"cats",msg))
# result = re.finditer("cats", msg)
# # print(result)
# for i in result:
#     print(i.group())

#########sub  替换
#sub(正则规则，替换内容，匹配的字符串)
# msg = "I'm learning python!"
# print(re.sub('learn','LEARN',msg))

########compile 编译正则
# msg = "I am learning python"
# msg2 = "welcome to sanchuang"
# msg3 = "python, hello world"
#
# reg = re.compile("python")
# print(reg.findall(msg))
# print(reg.findall(msg2))
# print(reg.findall(msg3))
#
# print(re.findall('python',msg))
# print(re.findall('python',msg2))
# print(re.findall('python',msg3))

###############正则表达式基本用法############
#区间   可以根据字符编码范围可以来确定字符范围
# ret = re.findall("[Ppfmt]ython","Python 3 python fython mython tython")
# print(ret)
# ret = re.findall("[a-z]","abcABC123-")
# print(ret)
# ret = re.findall("[A-z]","abcABC123-_")
# print(ret)
# ret = re.findall("[A-Za-z0-9]","abcABC123-_")
# print(ret)

#区间取反
# ret = re.findall("a[^a-z]","1ab3cd4loj234abl2a3b3")
# print(ret)

#匹配或
# msg = "It's raining cats and dogs"
# ret = re.search("cats|dogs", msg)
# print(ret.group())
# ret2 = re.findall("cats|dogs", msg)
# print(ret2)

#占位符  .  表示除了\n之外的任意字符
# ret = re.findall("p.thon","Python pthon pmthon p-thon p\nthon")
# print(ret)


#快捷方式
# \A 匹配字符串开始
# \bword\b 词边界
# \w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'
# \W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'
# \d 匹配一个数字字符。等价于 [0-9]
# \D 匹配一个非数字字符。等价于 [^0-9]
# \s 匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]
# \S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]
# ret = re.findall("\Apython[0-9]","python1 python2 python3")
# print(ret)

#\b  词边界
# ret = re.findall(r"world\b","hello world 123world world123 =world ##world## abcworldabc")
# print(ret)
#
# ret = re.findall(r"\bworld\b","hello world 123world world123 ##world##")
# print(ret)
#
# ret = re.findall(r"\Bworld","hello world 123world world123 ##world## ")
# print(ret)

# ret = re.findall(r"\w","python3#")
# print(ret)
# ret = re.findall(r"\W","python3#")
# print(ret)

# ##########匹配开始与结束
# 开始与结束：^, $
# • 匹配以python开头：^python
# • 匹配以python结尾：python$

# ret = re.findall("^python","hello python \npython")
# print(ret)
# ret = re.findall("^python","python3")
# print(ret)
#
# ret = re.findall("python$","hello python")
# print(ret)

#练习
#使用正则表达式，不显示test文本中以#，$开头的行，还不显示空行
# import re
# with open("test","r+") as fp:
#     for i in fp:
#         # print(i)
#         # print("###########################")
#         #ret=re.findall(r"^#|^\$|^\s", i)
#         #ret = re.findall(r"^(#|\$|\s)", i)
#         ret = re.findall(r"^[#$\s]",i)
#         #print(ret)
#         if not ret:
#             print(re.sub(r"\n","",i))

######################正则重复
#通配符
#？ 、 * 、 +
#？  匹配前一项0次或1次
# ret = re.findall("py?","appppython#!3p")
# print(ret)
# #*  匹配前一项0次或多次
# ret = re.findall("p*","ppppppython3#p")
# print(ret)
# #+ 匹配一次以上
# ret = re.findall("p+","ppppthon3p")
# print(ret)
# #{n,m} 指定匹配n到m次
# ret = re.findall("p{2,4}","pppppppppthonppppthonpp")
# print(ret)

#贪婪模式和非贪婪模式
#贪婪模式： 尽可能匹配多个
#非贪婪模式： 匹配到就返回
# msg = "sanchuang hello world 123 tongle 123"
# ret = re.findall("s.*123",msg)
# print(ret)
# ret = re.findall("s.*?123",msg)
# print(ret)
# ret = re.findall("p{2,4}?","ppppppthonpppthonpp")
# ret = re.findall("p{2}","ppppppthonpppthonpp")
# print(ret)

#练习
#匹配 正整数 负整数 浮点数
# while 1:
#     num = input("请输入数字：")
#     #if re.findall(r"^[+-]?\d+\.\d+$",num):
#     if re.findall(r"^[+-]?(\d\.\d+$|[1-9]\d*\.\d+$)",num):
#         print(num,"是浮点数")
#     #elif re.findall(r"^+?\d+$",num):
#     elif re.findall(r"^\+?(0+[1-9]+\d*|[1-9]\d*)$",num):
#         print(num,"是正整数")
#     elif re.findall(r"^-\d*$",num):
#         print(num,"是负整数")
#     else:
#         print("输入的不是数字")

#作业
#1、长度为8-10的用户密码（以字母开头，包含字母，数字，下划线）
#2、ip地址匹配
#3、邮箱匹配  （中间包含@   xxx@xx.(com,cn,mail) xxx都是以字母数字组合 @前面8-16位）
#4、电话号码验证（+086 13711111111） 可以加区号也可以不加 手机号以1开头，11位


##############正则分组####################
# msg = "tel: 173-7572-2991"
# ret = re.search(r'(\d{3})-(\d{4})-(\d{4})',msg)
# print(ret.group())
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))
# print(ret.groups())
#
# #捕获分组
# ret = re.findall(r'(\d{3})-(\d{4})-(\d{4})',msg)
# print(ret)

#分组向后引用
# str1 = "a1 d1 d1 d1"
# print(re.search(r"([a-z][0-9]) ([a-z][0-9]) \1 \2",str1).group())

#非捕获分组
# #只分组，不捕获，不会在内存中缓存分组匹配的值
# ret = re.search(r"(?:[a-z][0-9])\s+([a-z][0-9])\s+\1",str1)
# print(ret.group())
# print(ret.groups())

#findall 有捕获分组 只会显示捕获分组里的内容
# match.groups()
# ret = re.findall(r"(?:[a-z][0-9])\s+([a-z][0-9])\s+\1",str1)
# print(ret)

#把163.com   126.com  qq.com 的 邮箱地址获取出来
# msg = 'wcd12@163.comyyy@xxx.com12345678@qq.comaaccca@126.combbb@163.comcc@abc.com'
# #wcd12@163.com 12345678@qq.com aaacccca@126.com bbb@163.com
# ret = re.findall(r'(?:com)*(\w+@(?:qq|126|163)\.com)',msg)
# print(ret)

#输出test2  test3
# msg2 = "<html>test1<div>test2222</div><div><a>test33333</a></div></html>"
# ret = re.findall(r'(\w+)(?:</\w+>)',msg2)
# print(ret)

# ip = "12345"
# ret = re.findall(r"[1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-6]", ip)
# print(ret)

# while True:
#     ip = input("请输入正确的ip地址：")
#     ret = re.search(r'((1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))',ip)
#     print(ret.group())
#     if re.search(r'((1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))',ip):
#         print("ip地址格式正确")
#     else:
#         print("ip地址格式错误")

############命名分组
# msg = "hello 123 world 456"
# ret = re.search(r"(?P<sanle>\d{3})\s+(\w+)",msg)
# print(ret.group())
# print(ret.groupdict())
# ret = re.findall(r"(?P<sanle>\d{3})\s+(\w+)",msg)
# print(ret)

##正则标记
# msg = """
# PYTHON123
# python
# """

#大小写不敏感的标志位
# ret = re.findall(r"python",msg,re.I)
# print(ret)
# #多行匹配和大小写不敏感
# #多行匹配主要是影响^ 和 $
# ret = re.findall(r"^python$",msg,re.I|re.M)
# print(ret)

# .除换行符以外的任意字符
#re.S 让.表示任意字符包括换行符
# ret = re.findall(r".+",msg)
# print(ret)
# ret = re.findall(r".+",msg,re.S)
# print(ret)
#
# msg = """
# http://baidu.com
# https://abc.www.baidu.com
# https://www.baidu.com?a=1
# https://www.baidu.com/a/b/c
# """
#
#
# ret = re.findall(r'^(?:https?://)((?:\w+\.)+com)(?:\?.*|/.*|)', msg,re.M)
# print(ret)

#零宽断言
import re
s = "a regular expgular reegular"
print(re.findall(r"re(?=gular)",s))
print(re.findall(r"re(?!gular)",s))
print(re.findall(r"(?<=re)gular",s))
print(re.findall(r"(?<!re)gular",s))

s = "hello, i am python, 'sanchuang tongle' 'learning'"
ret = re.findall(r"(?<=\')\S+.+?(?=\')|\w+",s)
print(ret)

#爬取 https://www.sanchuangedu.cn/网站上的图片保存到本地
# import requests
# ret = requests.get("https://www.sanchuangedu.cn/")
# print(ret.text)

#src="/uploads/allimg/170812/1-1FQ2143HS18.jpg"
#https://www.sucai8.com/uploads/allimg/170812/1-1FQ2143HS18.jpg
#把图片保存到本地  open